import { appAddressListGet, appAddressDelPost, appAddressSetDefaultGet } from '@/api/address'
const useAddressStore = defineStore('address', {
  state: () => ({
    dataList: [],
    loading: false,
    total: 0,
    queryData: {
      type: '',
      pageNum: 1,
      pageSize: 12
    }
  }),
  actions: {
    setList () {
      this.loading = true
      return appAddressListGet({ body: this.queryData }).then((res) => {
        this.dataList = res.data
        return res.data.find(item => item.isDefault === 'Y')
      }).catch((e) => {
        console.log(e)
      }).finally(() => {
        this.loading = false
      })
    },
    deleteAddress (item) {
      return appAddressDelPost({ body: { id: item.id } }).then(res => {
        this.setList()
      }).catch((e) => {
        console.log(e)
      }).finally(() => {
      })
    },
    setAddressDefault (item) {
      return appAddressSetDefaultGet({ params: { id: item.id } }).then(res => {
        this.setList()
      }).catch((e) => {
        console.log(e)
      }).finally(() => {
      })
    },
    init () {
      return this.setList()
    }
  }
})

export default useAddressStore